import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_the_feign_exercise_plan_page_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

///应急演练清单
class EmergencyDrillListController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 演练内容
  String searchSelect = '';
  RxString searchX = ''.obs;

  List<GetDictAllEntity> currencyState = [];
  List<GetDictAllEntity> estimateType = [];
  List<GetDictAllEntity> periodValidity = [];

  /// 备案
  String currencyStateId = '';
  String currencyStateName = '请选择';

  /// 评估
  String estimateId = '';
  String estimateName = '请选择';

  /// 有效期
  String periodValidityId = '';
  String periodValidityName = '请选择';

  @override
  void onInit() {
    super.onInit();
    // 演练类别 0预案演示 1协同演练 2推演复盘
    initCurrencyState();
    // 演练类型 0桌面演练 1实战演练
    initEstimateType();
  }

  GetDictAllEntity? dictEntity1;
  GetDictAllEntity? dictEntity2;

  /// 是否备案 0是 1否
  void selectCurrencyDangerLevelType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '备案',
      dataList: currencyState,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
        currencyStateId = value[0].getId();
        currencyStateName = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  /// 是否评估 0是 1否
  void selectEstimateType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '评估',
      dataList: estimateType,
      checkedList: [dictEntity2 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity2 = value[0];
        estimateId = value[0].getId();
        estimateName = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  /// 获取数据
  Future<List<GetTheFeignExercisePlanPageEntity>> theFeignExercisePlanPage(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<GetTheFeignExercisePlanPageEntity>>(
      Interface.getTheFeignExercisePlanPage,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'exercisePlanName': searchX.value, // 演练计划名称
      },
    ).execute();
    List<GetTheFeignExercisePlanPageEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
    searchSelect = searchX.value; //向另一个地方插入值 那个地方也和这一样
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    searchX.value = '';
    searchSelect = '';
    riskRightScaffoldKey.currentState?.closeEndDrawer();
    globalKey.currentState!.onRefresh();
    update();
  }

  /// 确定
  void confirm() {
    Get.back();
    searchX.value = searchSelect;
    globalKey.currentState!.onRefresh();
    update();
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  void initCurrencyState() {
    currencyState.clear();
    final GetDictAllEntity getDictAllEntity = GetDictAllEntity();
    getDictAllEntity.id = '0';
    getDictAllEntity.dictionaryLabel = '预案演示';
    currencyState.add(getDictAllEntity);

    final GetDictAllEntity getDictAllEntity2 = GetDictAllEntity();
    getDictAllEntity2.id = '1';
    getDictAllEntity2.dictionaryLabel = '协同演练';
    currencyState.add(getDictAllEntity2);

    final GetDictAllEntity getDictAllEntity5 = GetDictAllEntity();
    getDictAllEntity5.id = '1';
    getDictAllEntity5.dictionaryLabel = '推演复盘';
    currencyState.add(getDictAllEntity5);
  }

  void initEstimateType() {
    estimateType.clear();
    final GetDictAllEntity getDictAllEntity3 = GetDictAllEntity();
    getDictAllEntity3.id = '0';
    getDictAllEntity3.dictionaryLabel = '桌面演练';
    estimateType.add(getDictAllEntity3);

    final GetDictAllEntity getDictAllEntity4 = GetDictAllEntity();
    getDictAllEntity4.id = '1';
    getDictAllEntity4.dictionaryLabel = '实战演练';
    estimateType.add(getDictAllEntity4);
  }
}
